/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.Typeofcollateral;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

/**
 *
 * @author Lee
 */
public class TypeofcollateralDAO extends DAO {

    public TypeofcollateralDAO() {
        super();
    }
    //BASIC DAO METHODS

    @SuppressWarnings("unchecked")
    public List<Typeofcollateral> getAll() throws Exception {
        List<Typeofcollateral> a = (List<Typeofcollateral>) getSession().createQuery("from Typeofcollateral a").setCacheable(true).list();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public Typeofcollateral get(int id) throws Exception {
        Typeofcollateral a = (Typeofcollateral) getSession().createQuery("from Typeofcollateral a where a.collateraltypeid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(Typeofcollateral a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(Typeofcollateral a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Typeofcollateral> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        criteria.setCacheable(true);
        List<Typeofcollateral> list = criteria.list();
        flush();
        return list;
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        int count = ((Integer) criteria.list().get(0)).intValue();
        flush();
        return count;
    }

    //OTHER METHODS
    @SuppressWarnings("unchecked")
    public Typeofcollateral getByType(String type) throws Exception {
        Typeofcollateral t = (Typeofcollateral) getSession().createQuery("from Typeofcollateral t where t.type=:type").setParameter("type", type).setMaxResults(1).setCacheable(true).uniqueResult();
        flush();
        return t;
    }

    @SuppressWarnings("unchecked")
    public boolean hasDuplicate(String type) throws Exception {
        Typeofcollateral t = (Typeofcollateral) getSession().createQuery("from Typeofcollateral t where t.type=:type").setParameter("type", type).uniqueResult();
        if (t == null) {
            return false;
        }
        return true;
    }
}
